package com.android.mangalib.activity;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.android.mangalib.adapter.CollectionAdapter;
import com.android.mangalib.adapter.CollectionAdapter.CollectionViewHolder;
import com.android.mangalib.activity.R;
import com.android.mangalib.constant.Constant;
import com.android.mangalib.object.Collection;
import com.android.mangalib.parser.XMLParser;

/**
 * Main activity, called when the application is launched
 * 
 */
public class MangalibActivity extends Activity {

	ListView mainListView;
	List<Collection> library;
	Button forecastButton;
	
	
    @Override
    /**
     * This method is called when the main activity is created, for instance when the application is launched
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mangalib);
        
        // Load the different widget and view in the mangalib.xml
        mainListView = (ListView) findViewById(R.id.mainListView);
        forecastButton = (Button) findViewById(R.id.forecastButton);
        library = new ArrayList<Collection>();
        
        try {
				XMLParser.addTome(getApplicationContext(), "132", "0", "0", "27/09/2012", "Naruto");
		} catch (Exception e) {
			Log.v("ERROR", e.getMessage());
		}
        
        
        try {
        	// Load all collections in order to create the list
	        library = XMLParser.getAllCollections(this);
	        
	        // The adapter permits to create the view of the collections
	        CollectionAdapter collectionAdapter = new CollectionAdapter(this, library);
	        mainListView.setAdapter(collectionAdapter);
	        
	        /**
	         * Manage the click on an item
	         * Creation of an anonymous class OnItemClickListener.
	         * Start a new activity which displays the selected collection
	         */
	        mainListView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> listViewParam, View viewParam,
						int positionParam, long idParam) {
					Intent intent = new Intent(getApplicationContext(), CollectionActivity.class);
					CollectionViewHolder holder = (CollectionViewHolder)(viewParam.getTag());
					// Put the name of the collection in order to put it as the title of the new activity
					intent.putExtra(Constant.COLLECTION_ACTIVITY, holder.tvTitle.getText());
					startActivity(intent);
				}
	        	
	        });
	        
	        /**
	         * Manage the click on the "forecast" button
	         * Creation of an anonymous class OnClickListener.
	         * Start a new activity which displays the forecast mangas
	         */
	        forecastButton.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Intent intent = new Intent(getApplicationContext(), ForecastActivity.class);
					startActivity(intent);
				}
			});
	        
		} catch (Exception e) {
			Log.v("ERROR", e.getMessage());
		}
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.mangalib, menu);
        return true;
    }
    
    
}
